package com.example.cloud.clients;

import com.example.cloud.model.Client;
import com.example.cloud.model.Result;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by wangtao on 2018/3/7.
 */
@FeignClient(name = "auth-center", fallbackFactory = AuthFeignClientFallbackFactory.class)
public interface AuthFeignClient {

    @RequestMapping(value = "/authorize", method = RequestMethod.POST)
    public Result authorized(@RequestParam("name") String name, @RequestParam("secret") String secret);

    @RequestMapping(value = "/getServiceList", method = RequestMethod.POST)
    public Result getServiceList(@RequestBody Client client);
}

@Component
class AuthFeignClientFallbackFactory implements FallbackFactory<AuthFeignClient>{
    @Override
    public AuthFeignClient create(Throwable throwable) {
        return new AuthFeignClient(){
            @Override
            public Result authorized(String name, String secret) {
                System.out.println(throwable);
                return null;
            }

            @Override
            public Result getServiceList(Client client) {
                System.out.println(throwable);
                return null;
            }
        };
    }
}
